//
//  ANGoodsListController.m
//  WeiYiMei
//
//  Created by iMac on 2019/4/23.
//  Copyright © 2019年 liyefu. All rights reserved.
//

#import "ANGoodsListController.h"
#import "GoodsSearchView.h"
#import "MainGoodsCell.h"
#import "ANGoodsInfoController.h"
#import "ANAppointmentPrepareController.h"

@interface ANGoodsListController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) GoodsSearchView *searchView;
@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, copy) NSString *searchText;
@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic) int pageNo;

@end

@implementation ANGoodsListController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.pageNo = 1;
    self.dataSource = @[].mutableCopy;
    self.searchText = @"";
    
    [self requestGoods];
}

- (void)setupSubViews
{
    self.searchView = [[GoodsSearchView alloc] init];
    [self.view addSubview:self.searchView];
    [self.searchView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.view);
        make.height.equalTo(@(44));
    }];
    WeakSelf
    self.searchView.searchHandler = ^(NSString * _Nonnull text) {
        weakSelf.searchText = text;
        weakSelf.pageNo = 1;
        [weakSelf requestGoods];
    };
    
    [self.view addSubview:self.tableView];
    [self.tableView registerClass:[MainGoodsCell class] forCellReuseIdentifier:@"好困啊"];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.searchView.mas_bottom);
        make.left.right.bottom.equalTo(self.view);
    }];
}

// 查询商户商品
- (void)requestGoods
{
    NSDictionary *body = @{
                           @"consumerId":self.consumerId,
                           @"goodsName":self.searchText,//商品名称 模糊查询
                           @"pageNo":@(self.pageNo),
                           @"pageSize":@20
                           };
    [URLManager.shareManager getUrlStr:Api_Shop_Trade_List parameters:body entityClass:[GoodsListEntity class] success:^(NSURLResponse * _Nonnull response, BaseEntity * _Nullable baseEntity, NSDictionary * _Nullable otherResponse) {
        
        if (baseEntity) {
            if (baseEntity.code.intValue == 200) {
                GoodsListEntity *data = baseEntity.data;
                if (self.pageNo == 1) {
                    [self.dataSource removeAllObjects];
                    [self.tableView.mj_footer resetNoMoreData];
                    [self.tableView.mj_header endRefreshing];
                    if (data.data.count < 20) {
                        self.tableView.mj_footer.alpha = 0;
                        [self.tableView.mj_footer endRefreshingWithNoMoreData];
                    }
                }
                else {
                    if (data.data.count < 20) {
                        [self.tableView.mj_footer endRefreshingWithNoMoreData];
                    }
                    else {
                        [self.tableView.mj_footer endRefreshing];
                    }
                }
                [self.dataSource addObjectsFromArray:data.data];
                [self.tableView reloadData];
            }
            else {
                [self.tableView.mj_header endRefreshing];
                [self.tableView.mj_footer endRefreshing];
            }
        }
        else {
            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer endRefreshing];
        }
        
    } failure:^(NSURLResponse * _Nullable response, NSError * _Nonnull error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    }];
}

#pragma mark - UITableViewDataSource,UITableViewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MainGoodsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"好困啊" forIndexPath:indexPath];
    cell.showShopName = false;
    cell.entity = self.dataSource[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.type == ANGoodsListControllerTypeShowMore) {
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
        GoodsEntity *goods = self.dataSource[indexPath.row];
        ANGoodsInfoController *vc = [[ANGoodsInfoController alloc] init];
        vc.goodsEntity = goods;
        [self.navigationController pushViewController:vc animated:YES];
    }
    else {
        if (self.didSelectGoodsHandler) {
            self.didSelectGoodsHandler(self.dataSource[indexPath.row]);
            [self.navigationController popViewControllerAnimated:YES];
        }
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return kWidth(100);
}


- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.tableFooterView = [UIView new];
        _tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            _tableView.estimatedRowHeight = 0;
            _tableView.estimatedSectionFooterHeight = 0;
            _tableView.estimatedSectionHeaderHeight = 0;
        }
        WeakSelf
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            weakSelf.pageNo = 1;
            [weakSelf requestGoods];
        }];
        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            weakSelf.pageNo++;
            [weakSelf requestGoods];
        }];;
    }
    return _tableView;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
